package com.module_live.view;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.utls.glide.GlideConfig;
import com.module_live.R;
import com.zyyoona7.popup.EasyPopup;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class BroadListPopup implements View.OnClickListener {

    private Activity activity;

    List<String> imgUrlList = new ArrayList<>();

    BroadListAdapter broadListAdapter;
//    BroadListAdapter1 broadListAdapter1;

    List<FrameLayout> linList = new ArrayList<>();

    public int clickPosition;

    public BroadListPopup(Activity activity) {

        this.activity = activity;

        initPopuPopu();
    }

    private EasyPopup popu;


    private void initPopuPopu() {
        popu = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.fragment_broadlist)
                .setWidth(WindowManager.LayoutParams.FILL_PARENT)
                .setHeight(WindowManager.LayoutParams.FILL_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);
                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }

    private TextView tvCancel, tvComfirm;
    private LinearLayout linAddBroad;
    private RecyclerView rvBroadList;

    private void initView(View view) {

        tvCancel = view.findViewById(R.id.tv_cancel);
        tvComfirm = view.findViewById(R.id.tv_comfire);
        linAddBroad = view.findViewById(R.id.lin_addBroad);
        rvBroadList = view.findViewById(R.id.rv_broadlist);

        tvCancel.setOnClickListener(this);
        tvComfirm.setOnClickListener(this);
        linAddBroad.setOnClickListener(this);

        broadListAdapter = new BroadListAdapter(activity, imgUrlList);

        MyLinearLayoutManager manager = new MyLinearLayoutManager(activity, MyLinearLayoutManager.HORIZONTAL, false);
        rvBroadList.setLayoutManager(manager);
        rvBroadList.setAdapter(broadListAdapter);
    }

    public void setBroadListUpdate(List<String> imgUrl) {
        if (imgUrlList.size() > 0) {
            imgUrlList.clear();

        }
        imgUrlList.addAll(imgUrl);
        broadListAdapter.notifyDataSetChanged();

    }


    public void dismiss() {
        popu.dismiss();
    }


    public void showAnswerPopuPopu(View view) {
        initPopuPopu();
        popu.showAtLocation(view, Gravity.TOP, 0, 0);

    }


    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.tv_cancel) {

            popu.dismiss();

        } else if (i == R.id.tv_comfire) {

            if (listener != null) {
                listener.showOneBroad(clickPosition);
            }

        } else if (i == R.id.lin_addBroad) {

            if (listener != null) {
                listener.addWhiteBroad();
            }

        }
    }

    public interface BroadListListener {

        void addWhiteBroad();

        void showOneBroad(int position);

        void onDeletePPTImage(int position);
    }

    public BroadListListener listener;

    public void setBroadListListener(BroadListListener broadListListener) {

        this.listener = broadListListener;

    }


    private void initWbListLinear() {
        for (FrameLayout frameLayout : linList) {
            frameLayout.setBackgroundResource(R.drawable.bg_ran_wblist_default);
        }
    }

//    class BroadListAdapter1 extends BaseAdapter<PPTDateBean> {
//
//        public BroadListAdapter1(Context context, List<PPTDateBean> mData) {
//            super(context, mData);
//        }
//
//        @Override
//        public int getLayoutId() {
//            return R.layout.item_broadlist;
//        }
//
//        @Override
//        protected void toBindViewHolder(final ViewHolder holder, final int position, final List<PPTDateBean> mData) {
//
//            final FrameLayout frWBList = holder.getView(R.id.fr_wblist);
//
//            final FrameLayout linBroad = holder.getView(R.id.lin_broad);
//            final ImageView broadImg = holder.getView(R.id.broadView);
//            ImageView ivDelete = holder.getView(R.id.iv_delete);
//            final LinearLayout linDelete = holder.getView(R.id.lin_delete);
//            //显示当白板的顺序
//            TextView tvNum = holder.getView(R.id.tv_num);
//            int num = position + 0;
//            tvNum.setText(num + "");
//
//            linList.add(frWBList);
//
//            if (!"".equals(mData.get(position).getPptImgUrl()) && !"null".equals(mData.get(position).getPptImgUrl())) {
//
//                Glide.with(activity)
//                        .load(mData.get(position).getPptImgUrl())
//                        .apply(GlideConfig.getRoundOptions(20))
//                        .into(broadImg);
//            }
//
//            frWBList.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    initWbListLinear();
//                    frWBList.setBackgroundResource(R.drawable.bg_ran_wblist_select);
//
//                    clickPosition = position;
//
//                }
//            });
//
//            broadImg.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    initWbListLinear();
//                    frWBList.setBackgroundResource(R.drawable.bg_ran_wblist_select);
//
//                    clickPosition = position;
//
//                }
//            });
//
//            frWBList.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//
//                    linDelete.setVisibility(View.VISIBLE);
//                    linBroad.setVisibility(View.GONE);
//
//                    return false;
//                }
//            });
//
//            ivDelete.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    if (listener != null) {
//                        listener.onDeletePPTImage(position);
//                    }
//
//                    imgUrlList.remove(position);
//                    broadListAdapter.notifyDataSetChanged();
//
//                }
//            });
//
//        }
//    }

    class BroadListAdapter extends RecyclerView.Adapter<BroadListHolder> {

        public Context context;
        public List<String> list;

        public BroadListAdapter(Context context, List<String> imgUrl) {
            this.context = context;
            this.list = imgUrl;
        }

        @Override
        public BroadListHolder onCreateViewHolder(ViewGroup view, int position) {

            View view1 = LayoutInflater.from(context).inflate(R.layout.item_broadlist, null);
            BroadListHolder listHolder = new BroadListHolder(view1);
            return listHolder;
        }

        @Override
        public void onBindViewHolder(final BroadListHolder holder, final int position) {

            linList.add(holder.frWBList);

            //设置当前白板页面，从1开始
            String num = position + 1 + "";
            holder.tvNum.setText(num);

            if (!"".equals(list.get(position)) && !"null".equals(list.get(position))) {

                Glide.with(context)
                        .load(list.get(position))
                        .apply(GlideConfig.getRoundOptions(20))
                        .into(holder.broadImg);
            }

            holder.broadImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    initWbListLinear();
                    holder.frWBList.setBackgroundResource(R.drawable.bg_ran_wblist_select);

                    clickPosition = position;

                }
            });

            holder.frWBList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    initWbListLinear();
                    holder.frWBList.setBackgroundResource(R.drawable.bg_ran_wblist_select);

                    clickPosition = position;

                }
            });

            holder.broadImg.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    holder.linDelete.setVisibility(View.VISIBLE);
                    holder.frBroad.setVisibility(View.GONE);
                    holder.frWBList.setBackgroundResource(R.drawable.bg_ran_wblist_default);

                    return false;
                }
            });

            holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (listener != null) {
                        listener.onDeletePPTImage(position);
                    }

                    imgUrlList.remove(position);
                    broadListAdapter.notifyDataSetChanged();

                }
            });


        }

        @Override
        public int getItemCount() {

            return list.size() > 0 ? list.size() : 0;
        }
    }


    class BroadListHolder extends RecyclerView.ViewHolder {

        public ImageView broadImg, ivDelete;
        public LinearLayout linDelete;
        public FrameLayout frWBList, frBroad;
        public TextView tvNum;

        public BroadListHolder(View itemView) {
            super(itemView);

            broadImg = itemView.findViewById(R.id.broadView);
            frWBList = itemView.findViewById(R.id.fr_wblist);
            frBroad = itemView.findViewById(R.id.lin_broad);

            ivDelete = itemView.findViewById(R.id.iv_delete);
            linDelete = itemView.findViewById(R.id.lin_delete);

            tvNum = itemView.findViewById(R.id.tv_num);
        }
    }


}
